package com.woniuxy.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayFundTransToaccountTransferRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;
import com.woniuxy.config.AlipayConfig;
import com.woniuxy.service.AlipayService;
import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.rm.tcc.api.LocalTCC;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author zhaoliancan
 * @description
 * @create 2019-08-08 18:49
 */
@Service

public class AlipayServiceImpl implements AlipayService {



    @Override
    public void aliPay(HttpServletResponse response, HttpServletRequest request,String order_number1,String total_amount1,String subject1) throws IOException {
        response.setContentType("text/html;charset=utf-8");

        PrintWriter out = response.getWriter();
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
        //设置请求参数
        AlipayTradePagePayRequest aliPayRequest = new AlipayTradePagePayRequest();
        //aliPayRequest.setReturnUrl(AlipayConfig.return_url);
        //aliPayRequest.setNotifyUrl(AlipayConfig.notify_url);

        //商户订单号，后台可以写一个工具类生成一个订单号，必填
        String order_number = order_number1;
        //付款金额，从前台获取，必填
        String total_amount = total_amount1;
        //订单名称，必填
        String subject = subject1;
        aliPayRequest.setBizContent("{\"out_trade_no\":\"" + order_number + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求
        String result = null;
        try {
            result = alipayClient.pageExecute(aliPayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        //输出
        out.println(result);
    }

    @Override
    public void zhuan(String out_biz_no1,String payee_account1,String payee_real_name1,String amount) throws Exception {
        System.out.println(out_biz_no1);
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do","2016101800713938","MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCZ2e38ymA9bhTCpdJu5K86JUu5hz+vJdm49NIBXkDnskjyUgjjUm28qXk52b4NCizpJMV5qWgsOeOeILR6JvImk4jK7Y24cpCJjqBQhcUnQNMNl/1442xmRshtSmtBhEYAY7Saajyz402KJ1cKaEfq+b+wgDwsjhooliSSmovaiIbQlxNHeCJTmK3hghZfTBj60HNQyrrnOzyI2iKrjJ72P/pKQCm4cWeiUexKizeeFXkvpIKtr2qt7K6TpkU279lXvPkJV8uOYhsMtT5b4WT7BN42X3eXCmWAz0UHuR/dVo7wrovTpoRHbJVO/p4ItJZbNrbxhGheAAa7dys/D2vLAgMBAAECggEBAIVWCBmT0wCqTLe9Orj83JhKBmzOOHU1GpJfKzogU6P6MZpBWo6Yu55MSlHG0h0++lk8kyCznyLlMM20jnVnF2ozZtRoOPDn6TguMeZXaNxyI/eeCAEjKKkjI0UEwBvR5VElGDrTY7SLhIH9QkQ0yE9vD6CKIpyCs00FgsDDy/0iB0ywKYnym0bLrRnARGEdu6T9sdVh8Gaz1jvFBHCvFg+RqbIJl4ZLt1M29Aqqsqtig2JwyGtXX6ZnuUFcyU9f55i+DCmeVPGPKWbm1cjx9mGMtpQlDdFke88YWfMAY31Kl+vKjS+7eYtEsP9nHmT/dS29F268PDfoznbVPsJkRTECgYEA3ws+U3uLgxlTNoGLqqnRFO0U+QXCgiRVmCdDwZ3vpB718CTzxLPIQ7dilTyAUvRb5FNvRoNl2GjMrOC2ybO+8kwJZO6/zlMtDX3ppbI6QXfLNJ1BYVr5y99acQqhiGsJZZ3zd92PWY9BIxXqYNNkCXrr9FTxv13l5XHd7UTPzKcCgYEAsJVwA6yuAu36abGOBjyPkwKA3ML181RaNAAb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ccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();
        request.setBizContent("{" +
                "\"out_biz_no\":\""+out_biz_no1+"\"," +
                "\"payee_type\":\"ALIPAY_LOGONID\"," +
                "\"payee_account\":\""+payee_account1+"\"," +
                "\"amount\":\""+amount+"\"," +
                "\"payee_real_name\":\""+payee_real_name1+"\"," +
                "\"remark\":\"转账备注\"" +
                "  }");
        AlipayFundTransToaccountTransferResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
            throw  e;
        }
        if(response.isSuccess()){
            System.out.println(response);
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            throw new Exception("调用失败");
        }
    }

    @Override
    public boolean commit(BusinessActionContext context) {
        System.out.println("---------");
        return true;
    }

    @Override
    public boolean rollback(BusinessActionContext context) {
        System.out.println("+++++++++++++++++++");
        return true;
    }
}

